package com.px.task.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.px.common.annotation.Log;
import com.px.common.core.controller.BaseController;
import com.px.common.core.domain.AjaxResult;
import com.px.common.core.page.TableDataInfo;
import com.px.common.enums.BusinessType;
import com.px.common.utils.poi.ExcelUtil;
import com.px.framework.aes.AesSecurityParameter;
import com.px.task.domain.AirportVideo;
import com.px.task.service.IAirportVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 机场直播参数Controller
 *
 * @author WRJ
 * @date 2024-08
 */
@Api("机场直播参数")
@RestController
@RequestMapping("/airport/video")
public class AirportVideoController extends BaseController
{
    @Autowired
    private IAirportVideoService airportVideoService;

    /**
     * 查询机场直播参数列表
     */
    @ApiOperation("查询机场直播参数列表")
//    @PreAuthorize("@ss.hasPermi('task:task:query')")
    @GetMapping("/list")
    @AesSecurityParameter(outEncode = true)
    public TableDataInfo list(AirportVideo airportVideo)
    {
        startPage();
        List<AirportVideo> list = airportVideoService.selectAirportVideoList(airportVideo);
        return getDataTable(list);
    }

    /**
     * 查询机场直播参数列表
     */
    @ApiOperation("查询摄像头索引")
    @GetMapping("/cameraList")
    @AesSecurityParameter(outEncode = true)
    public AjaxResult cameraList(@RequestParam("sn") String sn) {
        AirportVideo video = new AirportVideo();
        video.setGateway(sn);
        List<AirportVideo> videos = airportVideoService.selectAirportVideoList(video);
        if (CollectionUtil.isEmpty(videos)) {
            return AjaxResult.success(new ArrayList<>());
        } else {
            return AjaxResult.success(videos.stream().map(AirportVideo::getCameraIndex).collect(Collectors.toList()));
        }
    }

    /**
     * 查询机场直播参数列表
     */
    @ApiOperation("查询直播索引")
    @GetMapping("/videoList")
    @AesSecurityParameter(outEncode = true)
    public AjaxResult videoList(@RequestParam("sn") String sn, @RequestParam("cameraIndex") String cameraIndex) {
        AirportVideo video = new AirportVideo();
        video.setGateway(sn);
        video.setCameraIndex(cameraIndex);
        List<AirportVideo> videos = airportVideoService.selectAirportVideoList(video);
        if (CollectionUtil.isEmpty(videos)) {
            return AjaxResult.success(new ArrayList<>());
        } else {
            return AjaxResult.success(videos.stream().map(AirportVideo::getVideoIndex).collect(Collectors.toList()));
        }
    }

    /**
     * 导出机场直播参数列表
     */
    @ApiOperation("导出机场直播参数列表")
//    @PreAuthorize("@ss.hasPermi('task:task:query')")
    @Log(title = "机场直播参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @AesSecurityParameter(outEncode = false)
    public void export(HttpServletResponse response, AirportVideo airportVideo)
    {
        List<AirportVideo> list = airportVideoService.selectAirportVideoList(airportVideo);
        ExcelUtil<AirportVideo> util = new ExcelUtil<AirportVideo>(AirportVideo.class);
        util.exportExcel(response, list, "机场直播参数数据");
    }

    /**
     * 获取机场直播参数详细信息
     */
    @ApiOperation("获取机场直播参数详细信息")
//    @PreAuthorize("@ss.hasPermi('task:task:query')")
    @GetMapping(value = "/{id}")
    @AesSecurityParameter(outEncode = true)
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(airportVideoService.selectAirportVideoById(id));
    }

    /**
     * 新增机场直播参数
     */
    @ApiOperation("新增机场直播参数")
//    @PreAuthorize("@ss.hasPermi('task:task:edit')")
    @Log(title = "机场直播参数", businessType = BusinessType.INSERT)
    @PostMapping
    @AesSecurityParameter(outEncode = true)
    public AjaxResult add(@RequestBody AirportVideo airportVideo)
    {
        return toAjax(airportVideoService.insertAirportVideo(airportVideo));
    }

    /**
     * 修改机场直播参数
     */
    @ApiOperation("修改机场直播参数")
//    @PreAuthorize("@ss.hasPermi('task:task:edit')")
    @Log(title = "机场直播参数", businessType = BusinessType.UPDATE)
    @PutMapping
    @AesSecurityParameter(outEncode = true)
    public AjaxResult edit(@RequestBody AirportVideo airportVideo)
    {
        return toAjax(airportVideoService.updateAirportVideo(airportVideo));
    }

    /**
     * 删除机场直播参数
     */
    @ApiOperation("删除机场直播参数")
//    @PreAuthorize("@ss.hasPermi('task:task:remove')")
    @Log(title = "机场直播参数", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @AesSecurityParameter(outEncode = true)
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(airportVideoService.deleteAirportVideoByIds(ids));
    }
}
